package com.sky.controller;

import com.sky.result.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 通用接口
 */
@RestController
@RequestMapping("/admin/common")
@ApiOperation("文件上传")
@Slf4j
public class CommonController {
    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file) throws IOException {
      log.info("文件上传: {}",file);
      //原始文件名
        String originalFilename = file.getOriginalFilename();
      //截取原始文件名的后缀
      String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
      //构造上传文件名
        String objectName = UUID.randomUUID().toString() + suffix;
        if(!file.isEmpty()){
            file.transferTo(new File("D:\\images\\" + objectName));
            return Result.success();
        }
        else{
            return Result.error("上传失败");
        }
    }
}
